import requests
import json
from bs4 import BeautifulSoup
from zhipuai import ZhipuAI

def fetch_news_data():
    headers = {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "same-origin",
        "Referer": "https://tech.sina.com.cn/",
        "Referrer-Policy": "unsafe-url"
    }

    response = requests.get(
        "https://interface.sina.cn/pc_api/public_news_data.d.json?callback=jQuery1112012938329491215939_1733125602512&mod=pctech&cids=40809%2C240707&editLevel=0%2C1%2C2%2C3%2C4%2C6%2C7&cre=tianyi&merge=3&statics=1&length=15&up=0&down=0&tm=1733125602&action=0&top_id=14N6qH%2C14N5x5%2C14N5RW%2C14N4z7%2C14N5iw%2C14N4z9%2C%2C%2C&offset=0&ad=%7B%22rotate_count%22%3A100%2C%22platform%22%3A%22pc%22%2C%22channel%22%3A%22tianyi_pctech%22%2C%22page_url%22%3A%22https%3A%2F%2Ftech.sina.com.cn%2F%22%2C%22timestamp%22%3A1733125602565%7D&_=1733125602513",
        headers=headers
    )

    json_data = response.text
    json_data = json_data[json_data.index('(') + 1:json_data.rindex(')')]
    data = json.loads(json_data)
    return data

def extract_artibody_content(url, headers):
    content_response = requests.get(url, headers=headers)
    content_response.encoding = 'utf-8'
    soup = BeautifulSoup(content_response.text, 'html.parser')
    artibody = soup.select_one('#artibody')
    if artibody:
        return artibody.get_text(strip=True)
    return None

def zpqy_news_Summary(content):
    client = ZhipuAI(api_key="f6bdc32908c3cf8de43169e313138e70.KbWhKwD12tJcwd5y")  # 请填写您自己的APIKey
    response = client.chat.completions.create(
        model="glm-4-0520",
        messages=[
            {"role": "system", "content": """我会给你一篇新闻，请你总结这篇新闻，总结1-3点，内容简要最好不要超过一百字，不要带有主观情绪，以md格式输出，附加上链接，例如：实时新增用户反馈<font color="warning">132例</font>，请相关同事注意。
> 类型:<font color="comment">用户反馈</font>
> 普通用户反馈:<font color="comment">117例</font>
> VIP用户反馈:<font color="comment">15例</font>"""},
            {"role": "user", "content": content}
        ],
    )
    return response.choices[0].message.content

def main():
    data = fetch_news_data()
    headers = {
        "accept": "*/*",
        "accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
        "cache-control": "no-cache",
        "pragma": "no-cache",
        "sec-ch-ua": "\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Windows\"",
        "sec-fetch-dest": "document",
        "sec-fetch-mode": "navigate",
        "sec-fetch-site": "same-origin",
        "Referer": "https://tech.sina.com.cn/",
        "Referrer-Policy": "unsafe-url"
    }

    summaries = []
    for item in data.get("data", []):
        url = item.get("url")
        title = item.get("title")
        if url and title:
            content = extract_artibody_content(url, headers)
            if content:
                summary = zpqy_news_Summary(f"标题:{title},链接:{url},内容:{content}")
                summaries.append(f"## {title}\n{summary}")

    return "\n\n".join(summaries)



def send_info(url="", info=""):
    wx_url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={url}"
    resp = requests.post(wx_url,
        headers={"Content-Type":"application/json"},
        data=json.dumps({
                "msgtype": "markdown",
                "markdown": {
                    "content": info
                }
            })
        )
    print(info)
    
    return

if __name__ == "__main__":
    result = main()
    wx_url = "22c5cc1b-b56a-4a86-ab67-51ab24f29f01"
    send_info(wx_url, result)
